package com.atguigu.vod.service.impl;


import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.empty.VideoOss;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantPropertiesUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.CommitUploadRequest;
import com.tencentcloudapi.vod.v20180717.models.CommitUploadResponse;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;
import java.util.zip.CRC32;

/**
 * @author shkstart
 * @create 2022-09-28 16:53
 */
@Service
public class VodServiceImpl implements VodService {

    //上传视频
    @Override
    public VideoOss uploadVideo(MultipartFile uploadFile) {
        VodUploadClient client = new VodUploadClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        VodUploadRequest request = new VodUploadRequest();

        try {
            String filename = uploadFile.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf("."));
            File localFile = File.createTempFile("temp", suffix);
            if (!Objects.isNull(uploadFile)) {
                uploadFile.transferTo(localFile);
            }

            String absolutePath = localFile.getAbsolutePath();
            request.setMediaFilePath(absolutePath);

            VodUploadResponse response = client.upload("ap-guangzhou", request);
            String responseFileId = response.getFileId();

            VideoOss videoOss = new VideoOss(response.getFileId(), response.getMediaUrl());

            return videoOss;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "上传视频失败");
        }

    }

    //删除腾讯云视频
    @Override
    public void removeVideo(String videoId) {
        Credential cred = new Credential(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        VodClient client = new VodClient(cred, "ap-guangzhou");

        try {
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(videoId);
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }


    //删除多个腾讯云视频的方法
    @Override
    public void removeMoreVideo(List videoList) {
        //初始化对象
        Credential cred = new Credential(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        VodClient client = new VodClient(cred, "ap-guangzhou");

        try {
            //创建删除视频request对象
            DeleteMediaRequest req = new DeleteMediaRequest();

            System.out.println(videoList);
            //videoList值转换成 1,2,3
//            String videoIds = StringUtils.join(videoList.toArray(), ",");
//            System.out.println(videoIds);
            //向request设置视频id
            for (Object videoId : videoList) {
                req.setFileId(videoId.toString());
                client.DeleteMedia(req);
//                System.out.println(videoId);
//                req.setDeleteParts(longs);
                //调用初始化对象的方法实现删除
            }
//            req.setDeleteParts(v);

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }



    public static void main (String[]args){
            List<String> list = new ArrayList<>();
            list.add("11");
            list.add("22");
            list.add("33");
            String join = StringUtils.join(list.toArray(), ",");
            System.out.println(join);
        }
//    private String getRandom() {
//        Random random = new Random();
//        StringBuilder result = new StringBuilder();
//        for (int i = 0; i < 16; i++) {
//            result.append(random.nextInt(10));
//        }
//        return result.toString();
//    }
    }
